package cn.elwy.rcp.ui.widget;

/*
 * This is a simple test of com.snapps.swt.CButton functionality.
 */

import org.eclipse.jface.resource.ColorRegistry;
import org.eclipse.jface.resource.FontRegistry;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;

import cn.elwy.rcp.ui.widget.CButton;

public class CButtonTestDialog extends ApplicationWindow {
  private static String IMG1_PATH = "./icons/pinstripes.png";
  private static String IMG2_PATH = "./icons/tango-face-glasses.png";
  CButton inactiveButton;
  private CButton imageButton4_1;
  private Composite composite;

  public static void main(String[] args) {
    ApplicationWindow window = new CButtonTestDialog(null);
    window.setBlockOnOpen(true);
    window.open();
  }

  public CButtonTestDialog(Shell arg0) {
    super(arg0);
  }

  @Override
  protected Control createContents(Composite parent) {
    getShell().setText("com.snapps.swt.CButton Test");
    composite = new Composite(parent, SWT.BORDER);
    // composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_LIST_BACKGROUND));
    GridLayout gl_composite = new GridLayout(3, false);
    gl_composite.horizontalSpacing = 12;
    gl_composite.verticalSpacing = 8;
    composite.setLayout(gl_composite);
    // composite.setBackgroundImage(backgroundImage);
    // composite.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));

    // a button that toggles the inactive status of the second button
    CButton defaultButton = new CButton(composite, SWT.BORDER);
    defaultButton.setText("Default button style");
    defaultButton.addSelectionListener(new SelectionAdapter() {
      @Override
      public void widgetSelected(SelectionEvent event) {
        inactiveButton.setEnabled(!inactiveButton.getEnabled());
      }
    });

    inactiveButton = new CButton(composite, SWT.BORDER);
    inactiveButton.setEnabled(false);
    inactiveButton.setText("Inactive button");

    // a few examples of custom buttons (you can probably do better)
    CButton grayButton = getGrayButton(composite);
    grayButton.setText("&Custom Gray Button");

    CButton customButton = getCustomButton(composite);
    customButton.setText("Another Custom Button");

    // a small background image that can be tiled
    Image image = new Image(getShell().getDisplay(), IMG1_PATH);
    CButton imageButton = new CButton(composite, SWT.NONE);
    imageButton.setBorderWidth(10);
    imageButton.setText("Button With Cropped Image");
    imageButton.setBackgroundImage(image);

    CButton imageButton2 = new CButton(composite, SWT.BORDER);
    imageButton2.setText("Button With Centered Image");
    imageButton2.setBackgroundImage(image);
    imageButton2.setBackgroundImageStyle(CButton.BG_IMAGE_CENTER);

    CButton imageButton3 = new CButton(composite, SWT.BORDER);
    imageButton3.setText("Button With Stretched Image");
    imageButton3.setBackgroundImage(image);
    imageButton3.setBackgroundImageStyle(CButton.BG_IMAGE_STRETCH);

    CButton imageButton4 = new CButton(composite, SWT.BORDER);
    imageButton4.setText("Button With Tiled Image");
    imageButton4.setBackgroundImage(image);
    imageButton4.setBackgroundImageStyle(CButton.BG_IMAGE_TILE);
    imageButton4.setBorderWidth(10);

    // a larger background image that's bigger than the button itself
    image = new Image(getShell().getDisplay(), IMG2_PATH);
    imageButton = new CButton(composite, SWT.BORDER);
    imageButton.setText("Button With Default Image");
    imageButton.setBackgroundImage(image);
    // imageButton.setBackgroundImageStyle(CButton.BG_IMAGE_CROP);

    imageButton2 = new CButton(composite, SWT.BORDER);
    imageButton2.setText("Button With Centered Image");
    imageButton2.setBackgroundImage(image);
    imageButton2.setBackgroundImageStyle(CButton.BG_IMAGE_CENTER);
    new Label(composite, SWT.NONE);

    imageButton3 = new CButton(composite, SWT.BORDER);
    imageButton3.setText("Button With Stretched Image");
    imageButton3.setBackgroundImage(image);
    imageButton3.setBackgroundImageStyle(CButton.BG_IMAGE_STRETCH);

    imageButton4_1 = new CButton(composite, SWT.BORDER);
    GridData gd_imageButton4_1 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
    gd_imageButton4_1.heightHint = 89;
    gd_imageButton4_1.widthHint = 240;
    imageButton4_1.setLayoutData(gd_imageButton4_1);
    imageButton4_1.setText("Button With Tiled Image");
    imageButton4_1.setBackgroundImage(image);
    imageButton4_1.setBackgroundImageStyle(CButton.BG_IMAGE_TILE);
    new Label(composite, SWT.NONE);

    imageButton4 = new CButton(composite, SWT.BORDER);
    imageButton4.setText("Button With Side Image");
    imageButton4.setImage(image);

    imageButton4 = new CButton(composite, SWT.BORDER);
    imageButton4.setText("\n\n\nFit to Image");
    imageButton4.setBackgroundImage(image);
    imageButton4.setBackgroundImageStyle(CButton.BG_IMAGE_FIT);
    new Label(composite, SWT.NONE);

    // linefeeds in button data work too
    imageButton4 = new CButton(composite, SWT.BORDER);
    imageButton4.setText("Button \nWith \nSide \nImage ");
    imageButton4.setImage(image);
    imageButton4.setEnabled(false);

    imageButton4 = new CButton(composite, SWT.BORDER);
    imageButton4.setText("Button\nWith\nLine\nFeeds");
    new Label(composite, SWT.NONE);

    // buttons that stretch and resize inside a FillLayout
    Composite buttonBar = new Composite(composite, SWT.NONE);
    buttonBar.setLayout(new FillLayout());
    GridData data = new GridData(GridData.FILL_HORIZONTAL);
    data.grabExcessHorizontalSpace = true;
    data.horizontalSpan = 2;
    buttonBar.setLayoutData(data);

    CButton barButton = new CButton(buttonBar, SWT.BORDER);
    barButton.setRoundedCorners(false);
    barButton.setText("Fill Button 1");
    barButton = new CButton(buttonBar, SWT.BORDER);
    barButton.setRoundedCorners(false);
    barButton.setText("Fill Button 2");
    barButton = new CButton(buttonBar, SWT.BORDER);
    barButton.setRoundedCorners(false);
    barButton.setText("Fill Button 3");
    barButton = new CButton(buttonBar, SWT.BORDER);
    barButton.setRoundedCorners(false);
    barButton.setText("Fill Button 4");
    new Label(composite, SWT.NONE);

    return parent;
  }

  public CButton getGrayButton(Composite parent) {

    Canvas canvas = new Canvas(composite, SWT.NONE);
    Image itemSelectBgImage = SWTResourceManager
        .getImage(CButtonTestDialog.class, "/images/setting/item_select_bg.png");
    GridData gd_canvas = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 4);
    gd_canvas.heightHint = 128;
    canvas.setLayoutData(gd_canvas);
    canvas.setBackgroundImage(itemSelectBgImage);
    CButton button = new CButton(parent, SWT.BORDER);

    button.setInnerMarginHeight(6);
    button.setInnerMarginWidth(24);
    button.setBorderWidth(2);
    button.setSelectionBorder(true);

    FontData[] fontData = JFaceResources.getDialogFont().getFontData();
    Font font = getSavedFont(fontData[0].getName(), fontData[0].getHeight(), SWT.BOLD | fontData[0].getStyle());
    Color purple = getSavedColor(120, 80, 220);
    Color blue = getSavedColor(40, 40, 180);
    Color red = getSavedColor(200, 100, 50);
    Color grayStart = getSavedColor(220, 220, 220);
    Color grayEnd = getSavedColor(160, 160, 160);
    Color inactiveStart = getSavedColor(250, 250, 250);
    Color inactiveEnd = getSavedColor(220, 220, 220);
    Color hoverStart = getSavedColor(180, 180, 180);
    Color hoverEnd = getSavedColor(140, 140, 140);
    Color clickStart = hoverStart;
    Color clickEnd = hoverStart;

    button.setFont(font);
    button.setDefaultColors(grayStart, grayEnd, purple, purple);
    button.setHoverColors(hoverStart, hoverEnd, blue, blue);
    button.setSelectedColors(grayStart, grayEnd, purple, purple);
    button.setPressedColors(clickStart, clickEnd, red, red);
    button.setInactiveColors(inactiveStart, inactiveEnd, button.getBackground(), purple);

    return button;
  }

  public CButton getCustomButton(Composite parent) {
    CButton button = new CButton(parent, SWT.BORDER);

    button.setInnerMarginHeight(6);
    button.setInnerMarginWidth(24);
    button.setBorderWidth(10);
    button.setRoundedCorners(false);

    FontData[] fontData = JFaceResources.getDialogFont().getFontData();
    Font font = getSavedFont(fontData[0].getName(), fontData[0].getHeight() + 4, fontData[0].getStyle());
    Color white = getSavedColor(255, 255, 255);
    Color blueFont = getSavedColor(50, 100, 200);
    Color orangeFont = getSavedColor(200, 80, 80);
    Color lightBlueStart = getSavedColor(255, 255, 200);
    Color lightBlueEnd = getSavedColor(180, 180, 80);
    Color inactiveStart = getSavedColor(250, 250, 250);
    Color inactiveEnd = getSavedColor(220, 220, 220);
    Color hoverStart = getSavedColor(200, 255, 255);
    Color hoverEnd = getSavedColor(80, 180, 180);
    Color clickStart = hoverEnd;
    Color clickEnd = hoverEnd;

    button.setFont(font);
    button.setDefaultColors(lightBlueStart, lightBlueEnd, blueFont, blueFont);
    button.setHoverColors(hoverStart, hoverEnd, blueFont, blueFont);
    button.setSelectedColors(lightBlueStart, lightBlueEnd, orangeFont, orangeFont);
    button.setPressedColors(clickStart, clickEnd, white, white);
    button.setInactiveColors(inactiveStart, inactiveEnd, button.getBackground(), white);

    return button;
  }

  private Color getSavedColor(int r, int g, int b) {
    String colorString = "COLOR:" + r + "-" + g + "-" + b;
    ColorRegistry colorRegistry = JFaceResources.getColorRegistry();
    if (!colorRegistry.hasValueFor(colorString)) {
      colorRegistry.put(colorString, new RGB(r, g, b));
    }
    return colorRegistry.get(colorString);
  }

  private Font getSavedFont(String fontName, int fontSize, int fontStyle) {
    String fontString = "FONT:" + fontName + "-" + fontSize + "-" + fontStyle;
    FontRegistry fontRegistry = JFaceResources.getFontRegistry();
    if (!fontRegistry.hasValueFor(fontString)) {
      fontRegistry.put(fontString, new FontData[] { new FontData(fontName, fontSize, fontStyle) });
    }
    return fontRegistry.get(fontString);
  }
}
